package com.explore.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.explore.demo.common.ResultCode;
import com.explore.demo.common.ServerResponse;
import com.explore.demo.config.JwtConfig;
import com.explore.demo.mapper.UserMapper;
import com.explore.demo.pojo.CollectRelation;
import com.explore.demo.mapper.CollectRelationMapper;
import com.explore.demo.pojo.User;
import com.explore.demo.service.CollectRelationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gaolainan
 * @since 2021-04-21
 */
@Service
public class CollectRelationServiceImpl extends ServiceImpl<CollectRelationMapper, CollectRelation> implements CollectRelationService
{

    /**
     * 打印日志文件
     */
    private final Logger logger = LoggerFactory.getLogger(CollectRelationServiceImpl.class);


    @Resource
    private CollectRelationMapper collectRelationMapper;

    @Resource
    private JwtConfig jwtConfig;

    @Resource
    private UserMapper userMapper;

    @Override
    public ServerResponse addCollectRelation(@NotNull CollectRelation collectRelation)
    {
        return collectRelationMapper.insert(collectRelation) > 0 ?
               ServerResponse.createBySuccessMessage("插入成功",200,null):
               ServerResponse.createBySuccessMessage("插入失败",2000,null);
    }

    @Override
    public ServerResponse deleteByRecruitId(@NotNull Integer recruitId, HttpServletRequest request)
    {
        String token = request.getHeader(jwtConfig.getHeader());
        String account = jwtConfig.getUsernameFromToken(token);
        User user = userMapper.selectOne(new QueryWrapper<User>().eq("account", account));
        QueryWrapper<CollectRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("recruit_id",recruitId);
        wrapper.eq("user_id", user.getId());
        CollectRelation collectRelation = collectRelationMapper.selectOne(wrapper);
        return collectRelationMapper.deleteCollectRelation(collectRelation.getId()) == null?
               ServerResponse.createBySuccessMessage("success",200,null):
               ServerResponse.createBySuccessMessage("error",2000, null);
    }

    @Override
    public ServerResponse queryAll()
    {
        QueryWrapper<CollectRelation> wrapper = new QueryWrapper<>();
        wrapper
                .ge("id",1);
        return ServerResponse.createBySuccessMessage(ResultCode.SUCCESS.getDescription(),
                                                     ResultCode.SUCCESS.getCode(),
                                                     collectRelationMapper.selectList(wrapper));
    }

    @Override
    public ServerResponse queryAllByUserId(@NotNull Integer userId)
    {
        QueryWrapper<CollectRelation> wrapper = new QueryWrapper<>();
        wrapper
                .eq("user_id",userId);
        return ServerResponse.createBySuccessMessage(ResultCode.SUCCESS.getDescription(),
                                                     ResultCode.SUCCESS.getCode(),
                                                     collectRelationMapper.selectList(wrapper));
    }

    @Override
    public ServerResponse updateCollectRelation(@NotNull CollectRelation collectRelation)
    {
        return collectRelationMapper.updateById(collectRelation) != 0?
               ServerResponse.createBySuccessMessage(ResultCode.SUCCESS.getDescription(),
                                                     ResultCode.SUCCESS.getCode(),
                                                     null):
               ServerResponse.createBySuccessMessage(ResultCode.UPDATE_FAIL.getDescription(),
                                                     ResultCode.UPDATE_FAIL.getCode(),
                                                     null);
    }
}
